<?php
require_once('Puzzle.php');
require_once('Cell.php');
require_once('Section.php');

class Game
{
	const NUMBER_OF_CELLS = 81;
	const NUMBER_OF_ROWS = 9;
	const NUMBER_OF_SECTIONS = 9;
	
	const PUZZLE = 'puzzle.json';
	
	public function __construct()
	{
		$this->playGame();
	}
	
	public function playGame()
	{
			
		$puzzle = new Puzzle();
		$puzzle->initializePuzzle();
		/*
		$puzz = $this->loadPuzzle();

		for($count =0; $count < count($puzz); $count++)
		{
			if($puzz[$count] != "")
			{
				$puzzle->setCellValue($count, $this->getSectionNumber($count), $puzz[$count]);
			}
		}
		*/
		//Above will setup the puzzle
		
		for($i =0; $i < 81; $i++)
		{
			if(preg_match("/[0-9]/", $_POST[$i]))
			{
				$puzzle->setCellValue($i, $this->getSectionNumber($i), $_POST[$i]);
			}
		}
		
		$this->solvePuzzle($puzzle);
			
		$puzzle->printPuzzle();
	}
	
	public function solvePuzzle($puzzle)
	{
		$counter =0;
		$iteration =0;
		$puzzle_grid = $puzzle->getPuzzle();
		while(!$puzzle->isComplete() && $iteration <  100)
		{
			for($i = 0; $i < Game::NUMBER_OF_CELLS; $i++)
			{
				$sec = $this->getSectionNumber($i);
				$row = $puzzle->getRowNumber($i, $sec);
				$col = $puzzle->getColumnNumber($i, $sec);
				if($puzzle->isCellValueSet($i, $sec))
				{
					continue;
				}

				
				$puzzle->searchSectionForValues($i, $sec);
				if($puzzle->isCellValueSet($i, $sec))
				{
					continue;
				}
				
				$puzzle->searchRowValues($i, $row);
				if($puzzle->isCellValueSet($i, $sec))
				{
					continue;
				}
				
				$puzzle->searchColumnValues($i, $col);
				if($puzzle->isCellValueSet($i, $sec))
				{
					continue;
				}
				
				$puzzle->sectionElimination($i, $sec);
				if($puzzle->isCellValueSet($i, $sec))
				{
					continue;
				}
			}
				$iteration++;
				$counter++;
		}
		
		if($puzzle->isComplete())
		{
			printf("Puzzle Completed in $counter loops");
		}
	}
		
	public static function loadPuzzle()
	{
    	$cells = file_get_contents(Game::PUZZLE);
		
		$json_array = json_decode($cells, true);
		$cell_array = $json_array['puzzle'];
		$result_array = array();
		foreach($cell_array as $cell)
		{
			$value = $cell['value'];
			$result_array[] = $value;
		}
        return $result_array;;
	}
	
	public function getSectionNumber($cell_number)
	{
			//in section 0 ,1, or 2
			if($cell_number == 0      || $cell_number == 1  || $cell_number == 2
				|| $cell_number == 9  || $cell_number == 10 || $cell_number ==11
				|| $cell_number == 18 || $cell_number == 19 || $cell_number == 20)
			{
				return 0;
			}
			elseif($cell_number == 3  ||$cell_number == 4   ||$cell_number == 5
				|| $cell_number == 12 || $cell_number == 13 ||$cell_number == 14
				|| $cell_number == 21 || $cell_number == 22 || $cell_number == 23)
				{
					return 1;
				}
			elseif($cell_number == 6  || $cell_number == 7  || $cell_number == 8
				|| $cell_number == 15 || $cell_number == 16 || $cell_number == 17
				|| $cell_number == 24 || $cell_number == 25 ||$cell_number == 26)
				{
					return 2;
				}
			elseif($cell_number == 27 || $cell_number == 28 || $cell_number == 29
				|| $cell_number == 36 || $cell_number == 37 || $cell_number == 38
				|| $cell_number == 45 || $cell_number == 46 || $cell_number == 47)
				{
					return 3;
				}
			elseif($cell_number == 30 || $cell_number == 31 || $cell_number == 32
				|| $cell_number == 39 || $cell_number == 40 || $cell_number == 41
				|| $cell_number == 48 || $cell_number == 49 || $cell_number == 50)
				{
					return 4;
				}
			elseif($cell_number == 33 || $cell_number == 34 || $cell_number == 35
				|| $cell_number == 42 || $cell_number == 43 || $cell_number == 44
				|| $cell_number == 51 || $cell_number == 52 || $cell_number == 53)
				{
					return 5;
				}
			elseif($cell_number == 54 || $cell_number == 55 || $cell_number == 56
				|| $cell_number == 63 || $cell_number == 64 || $cell_number == 65
				|| $cell_number == 72 || $cell_number == 73 || $cell_number == 74)
				{
					return 6;
				}
			elseif($cell_number == 57 || $cell_number == 58 || $cell_number == 59
				|| $cell_number == 66 || $cell_number == 67 || $cell_number == 68
				|| $cell_number == 75 || $cell_number == 76 || $cell_number == 77)
				{
					return 7;
				}
			elseif($cell_number == 60 || $cell_number == 61 || $cell_number == 62
				|| $cell_number == 69 || $cell_number == 70 || $cell_number == 71
				|| $cell_number == 78 || $cell_number == 79 || $cell_number == 80)
				{
					return 8;
				}
	}
	
}

new Game();
